package carltechnology.com.mahjonggame.views.impl;

import android.app.Fragment;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import carltechnology.com.mahjonggame.R;
import carltechnology.com.mahjonggame.mode.event.FragmentViewEvent;
import carltechnology.com.mahjonggame.presenter.IMajhongGameInputPresenter;
import carltechnology.com.mahjonggame.presenter.impl.MahjongGameInputPresenter;
import carltechnology.com.mahjonggame.utils.Utils;
import carltechnology.com.mahjonggame.utils.views.TextEditView;
import carltechnology.com.mahjonggame.views.IMahjongGameInputView;

/**
 * Created by WZTCM on 2016/6/7.
 */
public class MahjonggameInputFragment extends Fragment implements IMahjongGameInputView{
    @InjectView(R.id.mahjonggame_input_button_cancel)
    Button cancelButton;
    @InjectView(R.id.mahjonggame_input_button_yes)
    Button yesButton;
    @InjectView(R.id.mahjonggame_input_player1)
    TextEditView editPlayer1;
    @InjectView(R.id.mahjonggame_input_player2)
    TextEditView editPlayer2;
    @InjectView(R.id.mahjonggame_input_player3)
    TextEditView editPlayer3;
    @InjectView(R.id.mahjonggame_input_player4)
    TextEditView editPlayer4;
    @InjectView(R.id.mahjonggame_input_perunit)
    TextEditView editViewPerUnit;
    private IMajhongGameInputPresenter iMajhongGameInputPresenter;
    public enum MSG_TYPE{
        SUCCESS_INPUT("输入正确，完成插入"),
        PLAYER1_UNCORRECT("请正确输入玩家1的姓名"),
        PLAYER2_UNCORRECT("请正确输入玩家2的姓名"),
        PLAYER3_UNCORRECT("请正确输入玩家3的姓名"),
        PLAYER4_UNCORRECT("请正确输入玩家4的姓名"),
        PERUNIT_UNCORRECT("请正确输入此局单价"),;
        private String msg;
        MSG_TYPE(String msg) {
            this.msg = msg;
        }
        @Override
        public String toString() {
            return msg;
        }
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public String getEditPlayer1() {
        return editPlayer1.getEditTextContent();
    }

    @Override
    public String getEditPlayer2() {
        return editPlayer2.getEditTextContent();
    }


    @Override
    public String getEditPlayer3() {
        return editPlayer3.getEditTextContent();
    }


    @Override
    public String getEditPlayer4() {
        return editPlayer4.getEditTextContent();
    }

    @Override
    public String getEditPerUnit() {
       // System.out.println(editViewPerUnit.getEditTextContent());
       return editViewPerUnit.getEditTextContent();
    }

    @Override
    public Fragment getSelf() {
        return this;
    }

    @Override
    public Handler getHandler() {
        return handler;
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.mahjonggame_input_fragment,container,false);
        ButterKnife.inject(this,view);
        iMajhongGameInputPresenter  = MahjongGameInputPresenter.newInstance(this);
        return view;

    }
    @OnClick(R.id.mahjonggame_input_button_yes)
    public void onButtonYes(){
        //
        iMajhongGameInputPresenter.examineAndStoreMahjongGameInfo();
        
    }
    @OnClick(R.id.mahjonggame_input_button_cancel)
    public void onButtonCancel(){
        Utils.getEventBus().post(new FragmentViewEvent(FragmentViewEvent.EVENT.EVENT_MAHJONG_MAIN_FRAGMENT));

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        iMajhongGameInputPresenter.endFragment();
    }

    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            MSG_TYPE msg_type = (MSG_TYPE) msg.obj;
            Snackbar.make(getView(),msg_type.toString(),Snackbar.LENGTH_SHORT)
                    .setAction("Undo", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                        }
                    })
                    .show();

        }
    };
}
